package tc.alert.tckejisoc.rule;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * author: cyq
 * time: 2024/10/23 17:37
 */
public class RegexMapping implements FieldMapping {
    private final String target;
    private final Pattern pattern;
    private final int group;

    public RegexMapping(String target, String regexPattern, int group) {
        this.target = target;
        this.pattern = Pattern.compile(regexPattern);
        this.group = group;
    }

    @Override
    public String extractValue(String rawLog) {
        Matcher matcher = pattern.matcher(rawLog);
        return matcher.find() ? matcher.group(group) : "";
    }

    @Override
    public String getTarget() {
        return target;
    }
}